﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSL_Tools.XMLTools;
using System.Xml;

namespace ReportingAppConfig
{
    public class AppConfiguration
    {
        #region -- Public Properties ----------------------------------------------------
        public XmlDocument xDoc { get; private set; }

        public PlotColors plotColors { get; private set; }
        public ApplicationProperties appProperties { get; private set; }
        public MachineAliases aliases { get; private set; }
        public ChartFormatPropertySets chartFormatPropertySets { get; private set; }
        public TableFormatPropertySets tableFormatPropertySets { get; private set; }
        public SingleRunReportBuilder singleRun { get; private set; }
        #endregion

        #region -- Private Properties ---------------------------------------------------
        private string _configurationSettingsFile;
        #endregion

        #region -- Constructors ---------------------------------------------------------
        public AppConfiguration(string configurationSettingsFile)
        {
            _configurationSettingsFile = configurationSettingsFile;
            xDoc = new XmlDocument();
            XmlHelper.LoadXMLFile(xDoc, "Configuration", _configurationSettingsFile);

            GetAllConfigurationSections();
        }
        #endregion

        #region -- Public Methods -------------------------------------------------------
        #endregion

        #region -- Private Methods ------------------------------------------------------
        private void GetAllConfigurationSections()
        {
            appProperties = new ApplicationProperties();
            appProperties.GetConfigValues(xDoc);

            aliases = new MachineAliases();
            aliases.GetConfigValues(xDoc);

            plotColors = new PlotColors();
            plotColors.GetConfigValues(xDoc);

            singleRun = new SingleRunReportBuilder();
            singleRun.GetConfigValues(ReplaceContextAliasesInDocumentSection(xDoc));

            chartFormatPropertySets = new ChartFormatPropertySets();
            chartFormatPropertySets.GetConfigValues(xDoc);

            tableFormatPropertySets = new TableFormatPropertySets();
            tableFormatPropertySets.GetConfigValues(xDoc);
        }

        private XmlDocument ReplaceContextAliasesInDocumentSection(XmlDocument inputDoc)
        {
            
            XmlDocument processedDoc = new XmlDocument();
            string str = inputDoc.OuterXml;
            processedDoc.LoadXml(str.Replace("{{numTopSlowest}}", "10"));
            return processedDoc;
        }
        #endregion

    }

    //xDoc.SelectSingleNode("Configuration/MachineAliases/MachineAliasSet[@Default = \"True\"]")

}
